package com.runvp.service.auth.service;

import com.runvp.common.constant.Constants;
import com.runvp.common.constant.UserConstants;
import com.runvp.common.enums.UserStatus;
import com.runvp.common.exception.base.BaseException;
import com.runvp.common.model.auth.BizUserBO;
import com.runvp.common.model.auth.LoginBody;
import com.runvp.common.model.auth.LoginUser;
import com.runvp.common.util.ToolUtils;
import com.runvp.common.util.auth.SecurityUtils;
import com.runvp.common.utils.IpUtils;
import com.runvp.common.utils.StringUtils;
import com.runvp.service.biz.domain.BizCustomer;
import com.runvp.service.biz.service.IBizCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 登录校验方法
 *
 * @author xkk update
 */
@Slf4j
@Component
public class LoginService {


    @Autowired
    private IBizCustomerService customerService;
    @Autowired
    private LoginLogService loginLogService;

    /**
     * 登录
     */
    public LoginUser login(String username,String password) {
        log.info("login username:{},password:{}", username,password);
        //校验随机验证码
        String ip = IpUtils.getHostIp();
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(username, password)) {
            loginLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写", ip);
            throw new BaseException("用户/密码必须输入");
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            loginLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围", ip);
            throw new BaseException( "用户名不在指定范围");
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            loginLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围", ip);
            throw new BaseException("用户密码不在指定范围");
        }
        // 查询用户信息
        BizCustomer user = customerService.selectBizCustomerByOpenId(username);
        log.info("login user:{}", user);
        if (StringUtils.isNull(user)) {
            loginLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在", ip);
            throw new BaseException("登录用户：" + username + " 不存在");
        }
        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            loginLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户已停用，请联系管理员", ip);
            throw new BaseException("对不起，您的账号：" + username + " 已停用,请联系管理员");
        }
        if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
            loginLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户密码错误", ip);
            throw new BaseException("密码不正确");
        }
        loginLogService.saveLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功", ip);
        return buildLoginUser(user);
    }

    private LoginUser buildLoginUser(BizCustomer customer) {
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(customer.getId());
        BizUserBO userBO = new BizUserBO();
        BeanUtils.copyProperties(customer,userBO);
        loginUser.setUserBO(userBO);
        return loginUser;
    }


}